//package com.jeffdouglas;
 package SendBoth;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.RequestToken;
 
public class LoginServlet extends HttpServlet {
 
  private static final Logger log = Logger.getLogger(LoginServlet.class);
 
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
 
    HttpSession session = req.getSession();
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(Credentials.TWITTER_CONSUMERKEY,Credentials.TWITTER_CONSUMERSECRET);
    RequestToken requestToken = null;
 
    try {
      requestToken = twitter.getOAuthRequestToken();
    } catch (TwitterException e) {
      log.error(e.toString());
    }
 
    // get the token and tokenSecret
    String token = (String)requestToken.getToken();
    String tokenSecret = (String)requestToken.getTokenSecret();
    // store the token and tokenSecret in the session
    session.setAttribute("token", token);
    session.setAttribute("tokenSecret", tokenSecret);
 
    // get the url that the user must click to authenticate w/OAuth
    String authUrl = requestToken.getAuthorizationURL();
    req.setAttribute("authUrl", authUrl);
    RequestDispatcher rd = req.getRequestDispatcher("login.jsp");
 
    try {
      rd.forward(req, resp);
    } catch (ServletException e) {
      log.error(e.toString());
    }
 
  }
}